<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use think\Db;

/**
 * 前台查看处理
 * @package app\index\controller
 */
class Index extends IndexBase
{
    # 初始化方法
    public function _initialize()
    {
        parent::_initialize();
        # 初始化首页框架
        # 获取栏目
        $category_list=model('Category')->getDataByWhere(['status'=>1]);
        $this->assign('category_list', $category_list);
        # 获取标签
        $tag_list=model('Tag')->getAllData();
        $this->assign('tag_list', $tag_list);
        # 置顶文章
        $top_list=model('Article')->getDataByWhere(['status&is_top'=>1], 'update_time DESC', 5);
        $this->assign('top_list', $top_list);
        # 获取友链
        $link_list=model('Link')->getDataByWhere(['status'=>1]);
        $this->assign('link_list', $link_list);
        # 获取个人信息
        $personalInfo = [
            'notification'=>config('web.site_notification'),
            'name' => config('web.site_author'),
            'avatar' => config('web.avatar'),
            'weibo' => config('web.weibo'),
            'github' => config('web.github'),
            'email' => config('web.mail'),
        ];
        $this->assign('other_info', $personalInfo);
    }
    # 首页
    public function index()
    {
        # 获取文章分页
        $article = model('Article')->getPage(['status'=>1], 5, 'create_time DESC');
        $this->assign('article_list', $article);
        return $this->fetch();
    }
    # 文章
    public function article()
    {
        if (!input('param.id')) {
            $this->error('对不起，该文章已删除或不存在', 'index/index/index');
        }
        $id=input('param.id');
        if (!$article=model('Article')->getDataByWhere(['id'=>$id,'status'=>1], '', '', true)) {
            $this->error('对不起，该文章已删除或不存在', 'index/index/index');
        }
        incVisits($id);
        $comment=getCommentList($id, '' );
        $this->assign('article', $article);
        $this->assign('comment_list', $comment);
        return $this->fetch();
    }
    # 栏目
    public function category()
    {
        if (!input('?param.flag')) {
            $this->redirect('index/index/category', ['flag'=>'default']);
        }
        $flag=input('param.flag');
        if (!$category=model('Category')->getDataByWhere(['flag'=>$flag], '', '', true)) {
                $this->error('不存在该栏目', 'index/index/index');
        }
        if (0===$count=model('Article')->getCount(['cid'=>$category['id'],'status'=>1])) {
            $this->assign('errmsg', '对不起，暂无相关文章');
        } else {
            $article = model('Article')->getPage(['cid'=>$category['id'],'status'=>1], 5, 'create_time DESC');
            $this->assign('article_list', $article);
        }
        $this->assign('category', $category);
        return $this->fetch();
    }
    # 标签
    public function tag()
    {
        if (!input('?param.flag')) {
            $this->redirect('index/index/tag', ['flag'=>'default']);
        }
        $flag=input('param.flag');
        if (!$tag=model('Tag')->getDataByWhere(['flag'=>$flag], '', '', true)) {
                $this->error('不存在该标签', 'index/index/index');
        }
        $allArticle=model('Article')->getDataByWhere(['status'=>1]);
        $ids=[];
        foreach ($allArticle as $value) {
            $tagsInfo=explode(",", $value['tid']);
            if (in_array($tag['id'], $tagsInfo)) {
                $ids[]=$value['id'];
            }
        }
        if (empty($ids)) {
            $this->assign('errmsg', '对不起，暂无相关文章');
        } else {
            $idStr=implode(',', $ids);
            $article = model('Article')->getPage(['id'=>['in',$idStr],'status'=>1], 5, 'create_time DESC');
            $this->assign('article_list', $article);
        }
        $this->assign('tag', $tag);
        return $this->fetch();
    }
    # 归档
    public function archive()
    {
        $archive=Db::query("select FROM_UNIXTIME(create_time, '%Y-%m') as time, count(*) as posts from ".config('database.prefix')."article where delete_time is null AND status=1 group by FROM_UNIXTIME(create_time, '%Y-%m') order by FROM_UNIXTIME(create_time, '%Y-%m') DESC");
        $this->assign('archive', $archive);
        return $this->fetch();
    }
    # 邮箱投稿
    public function email()
    {
        return $this->fetch();
    }
    # 关于页面
    public function about()
    {
        $data=model('Config')
            ->where('name', 'site_about')
            ->value('value');
        $this->assign('data', $data);
        return $this->fetch();
    }
    # 留言板
    public function message()
    {
        $data = model('Message')
            ->alias('m')
            ->field('m.*,u.nickname,u.avatar')
            ->join('__USER__ u', 'm.uid=u.id')
            ->where('m.status', 1)
            ->order('m.create_time DESC')
            // ->fetchSql(true)
            ->select();
        $this->assign('data', $data);
        // dump($data);
        return $this->fetch();
    }
    # 搜索
    public function search()
    {
        if (empty(input('get.keyword'))) {
            $this->assign('errmsg', '对不起，请输入搜索条件');
        } else {
            $keyword=input('get.keyword');
            $count=model('Article')->getCount(['title|content'=>['like','%'.$keyword.'%'],'status'=>1]);
            if (0===$count) {
                    $this->assign('errmsg', '对不起，暂无相关文章');
            } else {
                $result=model('Article')->getPage(['title|content'=>['like','%'.$keyword.'%'],'status'=>1], 5, 'create_time DESC');
                $this->assign('count', $count);
                $this->assign('article_list', $result);
            }
        }
        return $this->fetch();
    }
}
